<?php

namespace app\admin\controller;
use think\Db;
use app\common\service\BalanceService;
class Total extends Common{

	public function getYesDate(){
		return date('Y-m-d',strtotime('-1 days'));
	}

	public function getThreeDate(){
		return date('Y-m-d',strtotime('-3 days'));
	}

	public function getWeekDate(){
		return date('Y-m-d', time()-86400*date('w')+(date('w')>0?86400:-6*86400));
	}

	public function getMoothDate(){
		return date('Y-m-d', mktime(0,0,0,date('m'),1,date('Y')));
	}

	public function totalCheck(){
		$uid = $limit = request()->param('userid');
		$pid = $limit = request()->param('pid');
		if($pid != '') {
			if (!in_array($pid, $this->data_uids_arr)) {
				return ['code' => 1, 'msg' => '权限不足'];
			}
		}
		if($uid != '') {
			if (!in_array($uid, $this->data_uids_arr)) {
				return ['code' => 1, 'msg' => '权限不足'];
			}
		}
		return ['code' => 0, 'msg' => 'success'];
	}
	/*
	数据统计
	 */
	public function total(){
		$uid = request()->param('userid');
		$pid = request()->param('pid');
		$start_time = request()->param('start_time');
		$end_time   = request()->param('end_time');

		if($pid != '') {
			if (!in_array($pid, $this->data_uids_arr)) {
				$data_uids_arr = [0];
				// return ['code' => 1, 'msg' => '权限不足'];
			} else {
				$data_uids_arr = Db::table('tp_user_team')->where('uid',$pid)->column('x_uid');
				$data_uids_arr[] = $pid;
			}
			$uid = '';
			$this->assign('pid',$pid);
		} else {
			$pid = $this->admin_info['data_uid'];
			$data_uids_arr = $this->data_uids_arr;
		}
		if($uid != '') {
			if (!in_array($uid, $this->data_uids_arr)) {
				// return ['code' => 1, 'msg' => '权限不足'];
				$data_uids_arr = [0];
			} else {
				$data_uids_arr = [$uid];
			}
			$this->assign('userid',$uid);
		} else {
			if ($pid == '') {
				$pid = $this->admin_info['data_uid'];
				$this->assign('pid',$pid);
			}
		}

		if($start_time != '' && $end_time == '') {
            $end_time = '2030-01-01';
        }
        if($start_time == '' && $end_time != '') {
            $start_time = '2022-01-01';
        }
		if($start_time == '' && $end_time == '') {
            $start_time = '2022-01-01';
            $end_time = '2030-01-01';
        }
        $this->assign('start_time',$start_time);
        $this->assign('end_time',$end_time);

		// $time = strtotime(date("Y-m-d"),time());
		$today = date("Y-m-d",time());
		$yes = date('Y-m-d',strtotime('-1 days'));
		$three = date('Y-m-d',strtotime('-3 days'));
		$week = date('Y-m-d', time()-86400*date('w')+(date('w')>0?86400:-6*86400));
		$month = date('Y-m-d', mktime(0,0,0,date('m'),1,date('Y')));
		
		//注册统计
		$reg = [];
		$reg['total'] = Db::table('tp_user')->where('id','in',$data_uids_arr)->count();
		$reg['yes'] = Db::table('tp_user')->whereTime('reg_date','>',$yes)->whereTime('reg_date','<',$today)->where('id','in',$data_uids_arr)->count();
		$reg['today'] = Db::table('tp_user')->whereTime('reg_date','>',$today)->where('id','in',$data_uids_arr)->count();
		$reg['three'] = Db::table('tp_user')->whereTime('reg_date','>',$three)->where('id','in',$data_uids_arr)->count();
		$reg['week'] = Db::table('tp_user')->whereTime('reg_date','>',$week)->where('id','in',$data_uids_arr)->count();
		$reg['month'] = Db::table('tp_user')->whereTime('reg_date','>',$month)->where('id','in',$data_uids_arr)->count();
		$reg['time'] = Db::table('tp_user')->whereTime('reg_date','>',$start_time)->whereTime('reg_date','<',$end_time)->where('id','in',$data_uids_arr)->count();
		$this->assign('reg',$reg);

		//充值统计
		$into = [];
		$into['total'] = Db::table('tp_balance_record')->where('type',12)->where('uid','in',$data_uids_arr)->sum('balance')?:0;
		$into['yes'] = Db::table('tp_balance_record')->where('type',12)->whereTime('change_date','>',$yes)->whereTime('change_date','<',$today)->where('uid','in',$data_uids_arr)->sum('balance')?:0;
		$into['today'] = Db::table('tp_balance_record')->where('type',12)->whereTime('change_date','>',$today)->where('uid','in',$data_uids_arr)->sum('balance')?:0;
		$into['three'] = Db::table('tp_balance_record')->where('type',12)->whereTime('change_date','>',$three)->where('uid','in',$data_uids_arr)->sum('balance')?:0;
		$into['week'] = Db::table('tp_balance_record')->where('type',12)->whereTime('change_date','>',$week)->where('uid','in',$data_uids_arr)->sum('balance')?:0;
		$into['month'] = Db::table('tp_balance_record')->where('type',12)->whereTime('change_date','>',$month)->where('uid','in',$data_uids_arr)->sum('balance')?:0;
		$into['time'] = Db::table('tp_balance_record')->where('type',12)->whereTime('change_date','>',$start_time)->whereTime('change_date','<',$end_time)->where('uid','in',$data_uids_arr)->sum('balance')?:0;
		$this->assign('into',$into);


		//提币统计
		$out = [];
		$out['total'] = Db::table('tp_out_record')->where('status',1)->where('uid','in',$data_uids_arr)->sum('amount')?:0;
		$out['yes'] = Db::table('tp_out_record')->where('status',1)->whereTime('date','>',$yes)->whereTime('date','<',$today)->where('uid','in',$data_uids_arr)->sum('amount')?:0;
		$out['today'] = Db::table('tp_out_record')->where('status',1)->whereTime('date','>',$today)->where('uid','in',$data_uids_arr)->sum('amount')?:0;
		$out['three'] = Db::table('tp_out_record')->where('status',1)->whereTime('date','>',$three)->where('uid','in',$data_uids_arr)->sum('amount')?:0;
		$out['week'] = Db::table('tp_out_record')->where('status',1)->whereTime('date','>',$week)->where('uid','in',$data_uids_arr)->sum('amount')?:0;
		$out['month'] = Db::table('tp_out_record')->where('status',1)->whereTime('date','>',$month)->where('uid','in',$data_uids_arr)->sum('amount')?:0;
		$out['time'] = Db::table('tp_out_record')->where('status',1)->whereTime('change_date','>',$start_time)->whereTime('change_date','<',$end_time)->where('uid','in',$data_uids_arr)->sum('amount')?:0;
		$this->assign('out',$out);
		
		$out2 = [];
		$out2['total'] = Db::table('tp_out_record')->where('status',0)->where('uid','in',$data_uids_arr)->sum('amount')?:0;
		$out2['yes'] = Db::table('tp_out_record')->where('status',0)->whereTime('date','>',$yes)->whereTime('date','<',$today)->where('uid','in',$data_uids_arr)->sum('amount')?:0;
		$out2['today'] = Db::table('tp_out_record')->where('status',0)->whereTime('date','>',$today)->where('uid','in',$data_uids_arr)->sum('amount')?:0;
		$out2['three'] = Db::table('tp_out_record')->where('status',0)->whereTime('date','>',$three)->where('uid','in',$data_uids_arr)->sum('amount')?:0;
		$out2['week'] = Db::table('tp_out_record')->where('status',0)->whereTime('date','>',$week)->where('uid','in',$data_uids_arr)->sum('amount')?:0;
		$out2['month'] = Db::table('tp_out_record')->where('status',0)->whereTime('date','>',$month)->where('uid','in',$data_uids_arr)->sum('amount')?:0;
		$out2['time'] = Db::table('tp_out_record')->where('status',0)->whereTime('change_date','>',$start_time)->whereTime('change_date','<',$end_time)->where('uid','in',$data_uids_arr)->sum('amount')?:0;
		$this->assign('out2',$out2);
		//等级统计
		$level = [];
		$level_config = Db::table('tp_invest_level_config')->group('level')->field('level,name')->select();
		foreach ($level_config as $key => $val) {
			$val['total'] = Db::table('tp_user')->where('id','in',$data_uids_arr)->where('level',$val['level'])->count();
			$val['yes'] = Db::table('tp_user')->whereTime('vip_update_date','>',$yes)->where('level',$val['level'])->whereTime('vip_update_date','<',$today)->where('id','in',$data_uids_arr)->count();
			$val['today'] = Db::table('tp_user')->whereTime('vip_update_date','>',$today)->where('level',$val['level'])->where('id','in',$data_uids_arr)->count();
			$val['three'] = Db::table('tp_user')->whereTime('vip_update_date','>',$three)->where('level',$val['level'])->where('id','in',$data_uids_arr)->count();
			$val['week'] = Db::table('tp_user')->whereTime('vip_update_date','>',$week)->where('level',$val['level'])->where('id','in',$data_uids_arr)->count();
			$val['month'] = Db::table('tp_user')->whereTime('vip_update_date','>',$month)->where('level',$val['level'])->where('id','in',$data_uids_arr)->count();
			$val['time'] = Db::table('tp_user')->whereTime('vip_update_date','>',$start_time)->whereTime('vip_update_date','<',$end_time)->where('level',$val['level'])->where('id','in',$data_uids_arr)->count();
			$level[] = $val;
		}
		$this->assign('level',$level);
		
		//智能云统计
		$robot = [];
		$robot_config = Db::table('tp_invest_config')->where('id','>',0)->field('id,name')->select();
		foreach ($robot_config as $key => $val) {
			$val['total'] = Db::table('tp_invest_record')->where('status','in','1,2')->where('uid','in',$data_uids_arr)->where('config_id',$val['id'])->count();
			$val['yes'] = Db::table('tp_invest_record')->where('status','in','1,2')->whereTime('order_date','>',$yes)->where('config_id',$val['id'])->whereTime('order_date','<',$today)->where('uid','in',$data_uids_arr)->count();
			$val['today'] = Db::table('tp_invest_record')->where('status','in','1,2')->whereTime('order_date','>',$today)->where('config_id',$val['id'])->where('uid','in',$data_uids_arr)->count();
			$val['three'] = Db::table('tp_invest_record')->where('status','in','1,2')->whereTime('order_date','>',$three)->where('config_id',$val['id'])->where('uid','in',$data_uids_arr)->count();
			$val['week'] = Db::table('tp_invest_record')->where('status','in','1,2')->whereTime('order_date','>',$week)->where('config_id',$val['id'])->where('uid','in',$data_uids_arr)->count();
			$val['month'] = Db::table('tp_invest_record')->where('status','in','1,2')->whereTime('order_date','>',$month)->where('config_id',$val['id'])->where('uid','in',$data_uids_arr)->count();
			$val['time'] = Db::table('tp_invest_record')->where('status','in','1,2')->whereTime('order_date','>',$start_time)->whereTime('order_date','<',$end_time)->where('config_id',$val['id'])->where('uid','in',$data_uids_arr)->count();
			$robot[] = $val;
		}
		$this->assign('robot',$robot);

		//收益统计
		$profit = [];
		$profit_config = Db::table('tp_balance_record_type')->where('id','in',[102,103,104,105,106])->field('type,remark name')->select();
		foreach ($profit_config as $key => $val) {
			$val['total'] = Db::table('tp_balance_record')->where('type',$val['type'])->where('uid','in',$data_uids_arr)->sum('balance')?:0;
			$val['yes'] = Db::table('tp_balance_record')->where('type',$val['type'])->whereTime('change_date','>',$yes)->whereTime('change_date','<',$today)->where('uid','in',$data_uids_arr)->sum('balance')?:0;
			$val['today'] = Db::table('tp_balance_record')->where('type',$val['type'])->whereTime('change_date','>',$today)->where('uid','in',$data_uids_arr)->sum('balance')?:0;
			$val['three'] = Db::table('tp_balance_record')->where('type',$val['type'])->whereTime('change_date','>',$three)->where('uid','in',$data_uids_arr)->sum('balance')?:0;
			$val['week'] = Db::table('tp_balance_record')->where('type',$val['type'])->whereTime('change_date','>',$week)->where('uid','in',$data_uids_arr)->sum('balance')?:0;
			$val['month'] = Db::table('tp_balance_record')->where('type',$val['type'])->whereTime('change_date','>',$month)->where('uid','in',$data_uids_arr)->sum('balance')?:0;
			$val['time'] = Db::table('tp_balance_record')->where('type',$val['type'])->whereTime('change_date','>',$start_time)->whereTime('change_date','<',$end_time)->where('uid','in',$data_uids_arr)->sum('balance')?:0;
			$profit[] = $val;
		}
		$this->assign('profit',$profit);
		

		//投资统计
		// $profit = [];

		// $today = Db::table('tp_invest_profit_record')->whereTime('date','>=',date('Y-m-d'))->where('uid','in',$this->data_uids_arr)->field('COALESCE(sum(profit),0) profit,COALESCE(sum(invite_profit),0) invite_profit,COALESCE(sum(team_profit),0) team_profit,COALESCE(sum(total_profit),0) total_profit')->find();
		// $today['name'] = '今日';
		// $profit[] = $today;
		// $week = Db::table('tp_invest_profit_record')->whereTime('date','>=',date('Y-m-d',strtotime('-7 days')))->where('uid','in',$this->data_uids_arr)->field('COALESCE(sum(profit),0) profit,COALESCE(sum(invite_profit),0) invite_profit,COALESCE(sum(team_profit),0) team_profit,COALESCE(sum(total_profit),0) total_profit')->find();
		// $week['name'] = '七天';
		// $profit[] = $week;
		// $month = Db::table('tp_invest_profit_record')->whereTime('date','>=',date('Y-m-d',strtotime('-30 days')))->where('uid','in',$this->data_uids_arr)->field('COALESCE(sum(profit),0) profit,COALESCE(sum(invite_profit),0) invite_profit,COALESCE(sum(team_profit),0) team_profit,COALESCE(sum(total_profit),0) total_profit')->find();
		// $month['name'] = '一个月';
		// $profit[] = $month;
		// $total = Db::table('tp_invest_profit_record')->where('uid','in',$this->data_uids_arr)->field('COALESCE(sum(profit),0) profit,COALESCE(sum(invite_profit),0) invite_profit,COALESCE(sum(team_profit),0) team_profit,COALESCE(sum(total_profit),0) total_profit')->find();
		// $total['name'] = '总数';
		// $profit[] = $total;
		// $this->assign('profit',$profit);


		// $invest = [];
		// $today = Db::table('tp_invest_record')->whereTime('order_date','>=',date('Y-m-d'))->where('uid','in',$this->data_uids_arr)->field('COALESCE(sum(amount),0) amount')->find();
		// $today['name'] = '今日';
		// $invest[] = $today;
		// $week = Db::table('tp_invest_record')->whereTime('order_date','>=',date('Y-m-d',strtotime('-7 days')))->where('uid','in',$this->data_uids_arr)->field('COALESCE(sum(amount),0) amount')->find();
		// $week['name'] = '七天';
		// $invest[] = $week;
		// $month = Db::table('tp_invest_record')->whereTime('order_date','>=',date('Y-m-d',strtotime('-30 days')))->where('uid','in',$this->data_uids_arr)->field('COALESCE(sum(amount),0) amount')->find();
		// $month['name'] = '一个月';
		// $invest[] = $month;
		// $total = Db::table('tp_invest_record')->where('uid','in',$this->data_uids_arr)->field('COALESCE(sum(amount),0) amount')->find();
		// $total['name'] = '总数';
		// $invest[] = $total;
		// // var_dump($invest);die();
		// $this->assign('invest',$invest);

		return $this->fetch();
	}

}